/**
 * 微信公众平台开发模式(JAVA) SDK
 * (c) 2012-2013 ____′↘夏悸 <wmails@126.cn>, MIT Licensed
 * http://www.jeasyuicn.com/wechat
 */
package com.gson;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.lang3.StringUtils;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import com.gson.bean.Articles;
import com.gson.bean.InMessage;
import com.gson.bean.MusicOutMessage;
import com.gson.bean.NewsOutMessage;
import com.gson.bean.OutMessage;
import com.gson.bean.TextOutMessage;
import com.gson.inf.MessageProcessingHandler;
import com.gson.inter.LocateUtil;
import com.gson.inter.RssChild;
import com.gson.inter.RssViewer;
import com.gson.inter.TodayInHistoryService;
import com.gson.model.Music;
import com.gson.model.PosInfoModel;
import com.gson.util.JodaTime;
import com.gson.util.PosInfoHandler;
import com.gson.util.Tools;

/**
 * 自定义实现消息处理
 * @author GodSon
 *
 */
public class MessageProcessingHandlerImpl implements MessageProcessingHandler {

	@Override
	public OutMessage textTypeMsg(InMessage msg) {
		TextOutMessage oms = new TextOutMessage();
		StringBuffer replyMsg = new StringBuffer();
		String content = msg.getContent();
		if("?".equals(content)||"？".equals(content)){
			replyMsg.append("欢迎使用微信平台！");
			replyMsg.append("\r\n1、当前时间");
			replyMsg.append("\r\n2、听音乐");
			replyMsg.append("\r\n3、焦点新闻");
			replyMsg.append("\r\n4、历史上的今天");
			replyMsg.append("\r\n其他、回音壁请直接输入文字信息");
			oms.setContent(replyMsg.toString());
		}else if("1".equals(content)){
			oms.setContent(JodaTime.getNowTime());
		}else if("2".equals(content)){
			MusicOutMessage mom = new MusicOutMessage();
			String url = "http://bcs.duapp.com/yishi-music/%E5%9B%9E%E5%AE%B6.mp3?sign=MBO:97068c69ccb2ab230a497c59d528dcce:LdYZ%2FLXohKa6YCy9gbxL%2B1mZ4Co%3D";
			String url2 = "http://bcs.duapp.com/yishi-music/X-man.mp3?sign=MBO:97068c69ccb2ab230a497c59d528dcce:cYV%2B%2Fq2Tlv2de6gqecZynCyIm3k%3D";
			
			Music music = new Music();
			music.setTitle("回家|X-man");
			music.setDescription("萨克斯 回家  勾起你想家的愁思...");
			music.setMusicUrl(url);
			music.setHQMusicUrl(url2);
			
			mom.setMusic(music);
			mom.setFuncFlag(0);
			return mom;
		}else if("3".equals(content)){
			NewsOutMessage nom = new NewsOutMessage();
			List<Articles> articleList = new ArrayList<Articles>();
			String rss_url = "http://news.google.com/news?pz=1&cf=all&ned=cn&hl=zh-CN&output=rss";
			try {
				RssViewer new_viewer = new RssViewer(rss_url);
				ArrayList item_list = new_viewer.getRSS_items();
				Iterator itx = item_list.iterator();

				while (itx.hasNext()) {
					Articles articles = new Articles();
					RssChild rsschild = (RssChild) itx.next();
					articles.setTitle(rsschild.getTitle());
					String link = rsschild.getLink();
					link = link.substring(link.indexOf("&url=")+5);
					articles.setUrl(link);
					
					articleList.add(articles);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			nom.setArticles(articleList);
			nom.setFuncFlag(0);
			return nom;
		}else if("4".equals(content)){
			String info = TodayInHistoryService.getTodayInHistoryInfo();
			oms.setContent(info);
		}else if(Tools.checkCNet(content)){
			LocateUtil util = new LocateUtil();
			try {
				String rtnXml = util.getMobileLocateInfo(content);
				if(StringUtils.isEmpty(rtnXml)){
					oms.setContent("粗定位接口异常");
				}
				
				//解析返回结果
				PosInfoModel posInfo = null;
				SAXParserFactory factory = SAXParserFactory.newInstance();
				SAXParser parser = factory.newSAXParser();
				XMLReader xmlReader = parser.getXMLReader();
				PosInfoHandler handler = new PosInfoHandler();
				xmlReader.setContentHandler(handler);
				StringReader sr = new StringReader(rtnXml);
				InputSource is = new InputSource(sr);
				xmlReader.parse(is);
				posInfo = handler.getPosInfo();
				
				if(posInfo!=null && posInfo.isErrorFlag()==true){
					oms.setContent(posInfo.getErrorMsg());
				}else{
					NewsOutMessage nom = new NewsOutMessage();
					List<Articles> articleList = new ArrayList<Articles>();
					Articles articles = new Articles();
					String latlng = posInfo.getLATITUDE() + "," + posInfo.getLONGITUDE();
					StringBuilder sb = new StringBuilder();
					sb.append("http://maps.googleapis.com/maps/api/staticmap?center=" +
							latlng +
							"&zoom=17&size=640x320&markers=color:0xfd7a70%7Clabel:A%7C" +
							latlng +
							"&sensor=false");
					articles.setPicUrl(sb.toString());
					articles.setTitle(content+"  大致所在位置");
					articles.setDescription(util.getAddress(latlng));
					String url = "http://ditu.google.cn/maps?hl=zh-CN&mrt=loc&q=" + latlng;
					articles.setUrl(url);
					articleList.add(articles);
					
					nom.setArticles(articleList);
					nom.setFuncFlag(0);
					return nom;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			oms.setContent(content);
		}
		return oms;
	}

	@Override
	public OutMessage locationTypeMsg(InMessage msg) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public OutMessage imageTypeMsg(InMessage msg) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public OutMessage linkTypeMsg(InMessage msg) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public OutMessage eventTypeMsg(InMessage msg) {
		StringBuffer replyMsg = new StringBuffer();
		if("subscribe".equals(msg.getEvent())){
			TextOutMessage oms = new TextOutMessage();
			replyMsg.append("欢迎使用微信平台！");
			replyMsg.append("\r\n1、当前时间");
			replyMsg.append("\r\n2、听音乐");
			replyMsg.append("\r\n3、看图文");
			replyMsg.append("\r\n其他、回音壁请直接输入文字信息");
			oms.setContent(replyMsg.toString());
			return oms;
		}
		return null;
	}

	@Override
	public OutMessage voiceTypeMsg(InMessage msg) {
		// TODO Auto-generated method stub
		return null;
	}

}
